"use strict";
;(function(){
    var img1 = new Image(),
        img2 = new Image(),
        img3 = new Image(),
        img4 = new Image();
    img1.src= config.static_img+'/website/images/admin/icon_workorder_current.svg';
    img2.src = config.static_img+'/website/images/admin/icon_account_current.svg';
    img3.src = config.static_img+'/website/images/admin/icon_api_current.svg';
    img4.src = config.static_img+'/website/images/admin/icon_message_current.svg';
    var week = HQ.config.language.week[language];
    var month = HQ.config.language.month[language];

    var is_ajax = 0;
    var is_sea_val = 0;
    var sea_word = "";
    var search_id = "";
    var search_type = "";
    var search_city_id = "";
    var search_url_name = "";
    var mul_type = 0;
    var is_alias = "";
    var s_area_id = "";//存储区域ID
    var subarea_id_f = "";//二级区域
    var subarea_info_obj = "";
    var tmp_subarea_info_obj = "";
    var search_name = "";
    var search_lat = 0;
    var search_lan = 0;
    var is_complete = 0;
    var tmp_ajax = "";
    var poi_channel = "";
    var urlencode_val = "";
    /*$(".J_city_sel a").click(function(){

        //}
        if(/<i>/i.test($(this).html())){
            var city = $(this).html().replace(/^<span>(.*?)<i>.*?<\/span>$/i, "$1");
        }else{
            var city = $(this).html().replace(/^<span>(.*?)<\/span>$/i, "$1");
        }
        $("#J_city").val($(this).html()).css({color : "#333"}).parent().removeClass("index-date-error");
        $("#J_popup_city").hide();

        //增加alias
        search_type = "city";
        search_city_id = $(this).attr("data-val");
        search_url_name = $(this).attr("data-urlname");
        //from.datepicker("show");
        $("#J_tips").hide();
        return false;
    });*/

    // 防止相同事件快速重复触发
    var _timer = {};
    function delay_till_last(id, fn, wait) {
        if (_timer[id]) {
            window.clearTimeout(_timer[id]);
            delete _timer[id];
        }
        return _timer[id] = window.setTimeout(function() {
            fn();
            delete _timer[id];
        }, wait);
    }

    // 热门推荐点击
    $('#J_popup_city').on("click", ".se-recommend-hot-list a", function(){
        $("#J_tips").hide();
        if(/<i>/i.test($(this).html())){
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<i>.*?<\/span>$/i, "$1");
        }else{
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<\/span>$/i, "$1");
        }
        $("#J_city").val(city).css({color : "#333"}).removeClass("index-date-error");

        search_type = 'city';
        search_city_id = $(this).attr("data-val");
        search_url_name = $(this).attr("data-urlname");

        $("#J_popup_city").hide();
        return false;
    });
    // 最近搜索点击
    $('#J_popup_city').on("click", ".se-recently-se-list a", function(){
        $("#J_tips").hide();
        if(/<i>/i.test($(this).html())){
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<i>.*?<\/span>$/i, "$1");
        }else{
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<\/span>$/i, "$1");
        }
        $("#J_city").val(city).css({color : "#333"}).removeClass("index-date-error");

        search_type = 'city';
        search_city_id = $(this).attr("data-val");
        search_url_name = $(this).attr("data-urlname");

        $("#J_popup_city").hide();
        return false;
    });

    // 最近浏览、常订酒店
    $('#J_popup_city').on('click',".se-history-list .se-history-li,.se-frequently-hotel-list .se-frequently-hotel",function(e){
        $("#J_tips").hide();
        if(/<i>/i.test($(this).html())){
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<i>.*?<\/span>$/i, "$1");
        }else{
            var city = $(this).attr("data-name").replace(/^<span>(.*?)<\/span>$/i, "$1");
        }
        $("#J_city").val(city).css({color : "#333"}).removeClass("index-date-error");

        is_alias = $(this).attr("data-alias");
        search_type = "hotel";
        search_id = $(this).attr("data-id");
        search_city_id = $(this).attr("data-cityid");
        search_url_name = $(this).attr("data-urlname");
        search_name = $(this).attr("data-name");

        $("#J_popup_city").hide();
        var index = parseInt($(this).index()) + 1;
        tj && tj.sea_click($(this).attr("data-type") + "|推荐酒店:"+ $(this).attr("data-id") +":"+ index +"\thotellist");
        return false;
    });
    // 搜索弹窗查看更多 2018-7-3 daiem
    $(document).on('mouseenter',".ser-r-con", function(e){
        e.stopPropagation();
        $("#J_sea_box .ser-li").removeClass('ser-li-current');
        $('.ser-r-con').removeClass('ser-r-con-hover');
        $(this).addClass('ser-r-con-hover');
        var _this = $(this);
        if (_this.data('nums') > 0 && _this.siblings('.ser-r-con-more').is(':hidden')) {
            var ser_r_con_h_arr = [];
            $('#J_sea_box .ser-row').each(function(){
                ser_r_con_h_arr.push($(this).find('.ser-r-con').height());
            });
            $('.ser-row .ser-r-con-more').hide();
            $('#ser-r-con-more-tran').remove();
            // 计算定位
            var pos_container_top = 0, pos_triangle_top = 0;
            var ser_row_index    = _this.parents('.ser-row').index() - 1;
            var ser_r_con_more_h = _this.siblings('.ser-r-con-more').height();
            var current_this_h   = ser_r_con_h_arr[ser_row_index];
            if (ser_row_index > 0) {
                var sum_h = 0;
                for (var i = 0; i <= ser_row_index; i++) {
                    sum_h += ser_r_con_h_arr[i];
                }
                // 如果更多弹框高度小于等于父级框高度时
                if (ser_r_con_more_h <= current_this_h) {
                    pos_container_top = Math.round((current_this_h - ser_r_con_more_h) / 2);
                    pos_triangle_top  = Math.round((ser_r_con_more_h - 16) / 2);
                } else {
                    var current_this_mid_offset_top = Math.round(sum_h - current_this_h / 2);
                    // 如果更多弹框一半高度小于等于距顶父弹框的距离
                    if (current_this_mid_offset_top > Math.round(ser_r_con_more_h / 2)) {
                        pos_container_top = -Math.round((ser_r_con_more_h - current_this_h) / 2);
                        pos_triangle_top  = Math.round((ser_r_con_more_h - 16) / 2);
                    } else {
                        pos_container_top = current_this_h - sum_h;
                        pos_triangle_top  = Math.round(current_this_mid_offset_top - 8);
                    }
                }
            } else {
                if (current_this_h > ser_r_con_more_h) {
                    pos_container_top = (current_this_h - ser_r_con_more_h) / 2;
                    pos_triangle_top  = Math.round((ser_r_con_more_h - 16) / 2);
                } else {
                    pos_container_top = 0;
                    pos_triangle_top  = Math.round((current_this_h - 16) / 2);
                }
            }
            var _style = '<style id="ser-r-con-more-tran">.ser-r-con-more:before{box-sizing: content-box;width: 0px;height: 0px;position: absolute;top: ' + pos_triangle_top + 'px;left: -16px;padding:0;border-bottom:8px solid transparent;border-top:8px solid transparent;border-left:8px solid transparent;border-right:8px solid #FFFFFF;display: block;content:"";z-index: 12;}';
            _style += '.ser-r-con-more:after{box-sizing: content-box;width: 0px;height: 0px;position: absolute;top: ' + (pos_triangle_top - 1) + 'px;left: -18px;padding:0;border-bottom:9px solid transparent;border-top:9px solid transparent;border-left:9px solid transparent;border-right:9px solid #ececec;display: block;content:"";z-index:10}</span>';
            $('body').append(_style);
            $('#J_sea_box .ser-row .js_ser-r-con-hover').removeClass('js_ser-r-con-hover');
            _this.addClass('js_ser-r-con-hover').siblings('.ser-r-con-more').css('top', pos_container_top+'px').show();
        }
    }).on('mouseleave', ".ser-r-con", function(e){
        e.stopPropagation();
        var _this = $(this);
        setTimeout(function(){
            if (!_this.siblings('.ser-r-con-more').is(":hover")) {
                _this.removeClass('ser-r-con-hover').siblings('.ser-r-con-more').hide();
                $('#J_sea_box .ser-row .js_ser-r-con-hover').removeClass('js_ser-r-con-hover');
            } else {
                _this.siblings('.ser-r-con-more').unbind().on('mouseleave', function (e) {
                    setTimeout(function(){
                        $(this).hide().siblings('.ser-r-con').removeClass('js_ser-r-con-hover ser-r-con-hover');
                    }, 300);
                });
            }
        }, 300);
    });

    // 搜索弹窗最底部查看全部搜索结果 2018-7-3 daiem
    $("#J_sea_box").on('click', '.ser-row-bot', function(){
        $("#J_city").val(sea_word);
        var uri = "/cz_"+ encodeURIComponent(sea_word);
        window.location.href = uri;
        return false;
    });

    // 搜索弹窗元素鼠标移动背景样式切换 2018-7-5 daiem
    $(document).on('mouseover',"#J_sea_box .ser-li", function(){
        $("#J_sea_box .ser-li").removeClass('ser-li-current ser-li-current-top ser-li-current-bot ser-li-current-all');
        if ($(this).parents('div.ser-r-con-more').html()) {
            var slilen = $(this).parents('div.ser-r-con-more').find('.ser-li').length;
            var sliindex = $(this).index();
            if (slilen > 1) {
                if (sliindex === 0) {
                    $(this).addClass('ser-li-current ser-li-current-top');
                }else if ((sliindex + 1) === slilen) {
                    $(this).addClass('ser-li-current ser-li-current-bot');
                } else {
                    $(this).addClass('ser-li-current');
                }
            } else {
                $(this).addClass('ser-li-current ser-li-current-all');
            }
        } else {
            $(this).addClass('ser-li-current');
        }
    });

    // 搜索弹窗内容 2018-7-4 daiem
    $("#J_sea_box").on("click", '.ser-li', function(e){
        e.stopPropagation();
        sea_timer && clearTimeout(sea_timer);
        tmp_ajax.abort();
        restartflag = 0;
        search_is_complete = 0;
        $("#J_city").val($(this).attr("data-name")).css({color : "#333"}).parent().removeClass("index-date-error");
        $("#J_sea_box").hide();
        //增加alias
        is_alias = $(this).attr("data-alias");
        search_type = $(this).attr("data-type");
        search_id = $(this).attr("data-id");
        search_city_id = $(this).attr("data-cityid");
        search_url_name = $(this).attr("data-urlname");
        search_name = $(this).attr("data-name");
        //Add POI
        if(search_type=='poi'){
            search_lat = $(this).attr("data-lat");
            search_lan = $(this).attr("data-lan");
            poi_channel = "|"+$(this).attr("data-channel");
        }
        var chkin = $("#nav_from").val();
        var chkout = $("#nav_to").val();
        var room = 1;
        var adult = 2;
        var child = 0;
        var child_age = "";

        // 判断是否是搜索更多中的点击, 并记录日志
        var current_index = $(this).index() + 1, current_conp_index = $(this).parents('.ser-row').index(), iter_index = 1, total_sl_len = 0;
        if ($(this).parent().hasClass('ser-r-con-more')) {
            // 计算点击位置
            $('#J_sea_box .ser-row').each(function(){
                var sl_len   = $(this).find('.ser-m-con .ser-li').length;
                var sl_m_len = $(this).find('.ser-r-con').data('nums');
                total_sl_len += sl_len;
                if (iter_index == current_conp_index) {
                    current_index += total_sl_len;
                }
                total_sl_len += sl_m_len;
                ++iter_index;
            });
            tj && tj.sea_click("select_more|"+ $(this).attr("data-name") +"\tadmin\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+search_type+poi_channel+"|"+search_id+"\t"+current_index+"|"+total_sl_len);
        } else {
            // 计算点击位置
            $('#J_sea_box .ser-row').each(function(){
                var sl_len = $(this).find('.ser-m-con .ser-li').length;
                if (iter_index == current_conp_index) {
                    current_index += total_sl_len;
                }
                total_sl_len += sl_len;
                ++iter_index;
            });
            tj && tj.sea_click("select|"+ $(this).attr("data-name") +"\tadmin\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+search_type+poi_channel+"|"+search_id+"\t"+current_index+"|"+total_sl_len);
        }
        is_sea_val = 0;
    });

    // 全部搜索结果下划线 2018-7-27 daiem
    $(document).on('mouseenter', "#J_sea_box .ser-row-bot", function () {
        $("#J_sea_box .ser-li").removeClass('ser-li-current');
        $("#J_sea_box .ser-row-bot span").css('text-decoration', 'underline');
    }).on('mouseleave', "#J_sea_box .ser-row-bot", function () {
        $("#J_sea_box .ser-row-bot span").css('text-decoration', 'none');
    });

    // 禁止搜索框非点击区域点击时事件冒泡 2018-7-27 daiem
    $("#J_sea_box,#J_popup_city").click(function(e){
        e.stopPropagation();
    });

    //tips索引
    var key_index = 0;
    //城市输入框失去焦点
	$("#J_city").val($("#J_city").attr("data-placeholder")).css({color: "#999"});
    $("#J_city").click(function(e){
        $(".J_room_sel_win").hide();
        $(".J_sel_num").attr("data-isshow",0);
        $(".J-room-pro").attr("data-isshow",0);
        e.stopPropagation();
        $(this).select();

        if(is_sea_val == 0){
            $.ajax({
                type:"post",
                url:"/ajax/get_search_recommend_hotel_v1",
                data: {},
                dataType: 'json',
                async : false,
                success:function(data){
                    if(data.res == 0){
                        // 最近浏览recent_se_html
                        var is_history_show = false;
                        if(data.msg.history_html != ''){
                            $("#J_popup_city .se-history-list").html(data.msg.history_html).parents(".se-row").show();
                            $("#J_popup_city .index-se-con-recent").show();
                            tj && tj.sea_click("is_history|热门酒店:1\thomepage");
                            is_history_show = true;
                        }else{
                            $("#J_popup_city .se-history-list").parents(".se-row").hide();
                            tj && tj.sea_click("is_history|热门酒店:0\thomepage");
                        }
                        // 常订酒店
                        if(data.msg.frequently_html != ''){
                            $("#J_popup_city .se-frequently-hotel-list").html(data.msg.frequently_html).parents(".se-row").show();
                            $("#J_popup_city .index-se-con-recent").show();
                            tj && tj.sea_click("is_frequently|热门酒店:1\thomepage");
                            if (is_history_show) {
                                $("#J_popup_city .se-frequently-hotel-list").parents(".se-row").addClass('m-t13');
                            } else {
                                $("#J_popup_city .se-frequently-hotel-list").parents(".se-row").removeClass('m-t13');
                            }
                        }else{
                            $("#J_popup_city .se-frequently-hotel-list").parents(".se-row").hide();
                            tj && tj.sea_click("is_frequently|热门酒店:0\thomepage");
                        }
                        // 推荐城市
                        if(data.msg.recommend_city_html != ''){
                            $("#J_popup_city .se-recommend-hot-list").html(data.msg.recommend_city_html);
                        }
                        // 最近浏览
                        if(data.msg.recent_se_html != ''){
                            $("#J_popup_city .se-recently-se-list").html(data.msg.recent_se_html).parents(".se-row").show();
                            // tj && tj.sea_click("is_history|热门酒店:1\thomepage");
                        }else{
                            $("#J_popup_city .se-recently-se-list").parents(".se-row").hide();
                            // tj && tj.sea_click("is_history|热门酒店:0\thomepage");
                        }
                    }
                }
            });
            $("#J_popup_city").show();
            $("#J_sea_box").hide();
        }else{
            $("#J_popup_city").hide();
            $("#J_sea_box").show();
        }
    }).click(function(e){
        e.stopPropagation();
        $(this).select();
    }).keyup(function(e){
        var val = $(this).val();
        /*if(val.length >3 && tmp_ajax != ""){
            tmp_ajax.abort();
        }*/
        $("#J_city").parent().removeClass("index-date-error");
        $("#J_sel_citizenship_select").hide();
        var obj_current = $('#J_sea_box .ser-row .ser-m-con .ser-li.ser-li-current');
        if(is_sea_val != 0 && $("#J_sea_box").css("display") != "none" && sea_word != ""){
            var li_obj = obj_current;
            // 向上
            if (e.keyCode == 38) {
                if (!obj_current.html()) {
                    var ser_row_nums = $('#J_sea_box .ser-row').length;
                    var end_ser_li_nums = $('#J_sea_box .ser-row:eq(' + ser_row_nums + ') .ser-m-con .ser-li').length;
                    obj_current = $('#J_sea_box .ser-row:eq(' + ser_row_nums + ') .ser-m-con .ser-li:eq(' + end_ser_li_nums + ')');
                    obj_current.addClass('ser-li-current');
                } else {
                    var this_el_index = obj_current.index();
                    if ((this_el_index - 1) >= 0) {
                        obj_current = obj_current.removeClass('ser-li-current').prev().addClass('ser-li-current');
                    } else {
                        if (obj_current.parents('.ser-row').prev().hasClass('ser-row')) {
                            obj_current = obj_current.removeClass('ser-li-current').parents('.ser-row').prev().find(".ser-m-con .ser-li:last").addClass('ser-li-current');
                        }
                    }
                }
                var em_html = obj_current.html();
                obj_current.find('.ser-m-con-right').remove();
                var _html = obj_current.text();
                obj_current.html(em_html);
                $("#J_city").val(_html);
                return false;
            }
            // 向下
            if (e.keyCode == 40) {
                if (!obj_current.html()) {
                    obj_current = $("#J_sea_box .ser-row:eq(0) .ser-m-con .ser-li:eq(0)").addClass('ser-li-current');
                } else {
                    var this_el_nums = obj_current.parents('.ser-m-con').find('.ser-li').length;
                    var this_el_index = obj_current.index() + 1;
                    if ((this_el_index + 1) <= this_el_nums) {
                        obj_current = obj_current.removeClass('ser-li-current').next().addClass('ser-li-current');
                    } else {
                        if (obj_current.parents('.ser-row').next().hasClass('ser-row')) {
                            obj_current = obj_current.removeClass('ser-li-current').parents('.ser-row').next().find(".ser-m-con .ser-li:eq(0)").addClass('ser-li-current');
                        }
                    }
                }
                var em_html = obj_current.html();
                obj_current.find('.ser-m-con-right').remove();
                var _html = obj_current.text();
                obj_current.html(em_html);
                $("#J_city").val(_html);
                return false;
            }
            if(e.keyCode == 13){
				var chkin = $("#nav_from").val();
				var chkout = $("#nav_to").val();
				var room = 1;
				var adult = 2;
				var child = 0;
				var child_age = "";
				/*if(key_index == -1){
					var select_index = 0;
				}else{
					var select_index = key_index;
				}*/
				var type = li_obj.attr("data-type");
				var data_name = li_obj.attr("data-name");
				var data_id = li_obj.attr("data-id");
                if(type == 'poi'){
                    poi_channel = "|"+li_obj.attr("data-channel");
                }
				if(type == undefined){
					type = 'keywords';
					data_name = $("#J_city").val();
					data_id = 0;
				}
                var current_index = li_obj.index();
                var current_conp_index = li_obj.parents('.ser-row').index();
                var iter_index = 1,total_sl_len = 0;
                $('#J_sea_box .ser-row').each(function(){
                    var sl_len = $(this).find('.ser-m-con .ser-li').length;
                    if (iter_index == current_conp_index) {
                        current_index += total_sl_len;
                    }
                    total_sl_len += sl_len;
                    ++iter_index;
                });
				tj && tj.sea_click("select|"+ data_name +"\tadmin\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+type+poi_channel+"|"+data_id+"\t"+current_index+"|"+total_sl_len);
				/*if(li_obj.eq(key_index).attr("data-last") == 1){
					var uri = "/cz_"+ encodeURIComponent($("#J_city").val());
					window.location.href = uri;
					return false;
				}*/
				li_obj.find('.ser-m-con-right').remove();
				$("#J_city").val(li_obj.text()).css({color : "#333"}).parent().removeClass("index-date-error");
				$("#J_sea_box").hide();
				$("#from").focus().parent().addClass("index-date-current");
				from.datepicker("show");
				is_alias = li_obj.attr("data-alias");
				search_type = li_obj.attr("data-type");
				search_id = li_obj.attr("data-id");
				search_city_id = li_obj.attr("data-cityid");
				search_url_name = li_obj.attr("data-urlname");
                search_name = li_obj.attr("data-name");
                //Add POI
				if(search_type=='poi'){
                    search_lat = li_obj.attr("data-lat");
                    search_lan = li_obj.attr("data-lan");
                    poi_channel = "|"+li_obj.attr("data-channel");
                }
				is_sea_val = 0;
                return false;
            }
        }
        // 用户停止输入300ms后开始发ajax请求
        //delay_till_last('check_input', function() {
        //    search(val);
        //}, 300);
        stamp = (new Date()).valueOf();
        search(val);
    }).bind('paste', function(e) {
        var el = $(this);
        setTimeout(function() {
            var val = el.val();
            $(this).css({color : "#333"});
            urlencode_val = encodeURIComponent(val);
            urlencode_val = urlencode_val.replace('*', '%2A');
            $("#J_from").attr("action", "cz_"+ urlencode_val);
            if(is_sea_val != 0 && $("#J_sea_box").css("display") != "none" && sea_word != ""){
                var obj_current = $('#J_sea_box .ser-row .ser-m-con .ser-li.ser-li-current');
                var li_obj = obj_current;
                // 向上
                if (e.keyCode == 38) {
                    if (!obj_current.html()) {
                        var ser_row_nums = $('#J_sea_box .ser-row').length;
                        var end_ser_li_nums = $('#J_sea_box .ser-row:eq(' + ser_row_nums + ') .ser-m-con .ser-li').length;
                        obj_current = $('#J_sea_box .ser-row:eq(' + ser_row_nums + ') .ser-m-con .ser-li:eq(' + end_ser_li_nums + ')');
                        obj_current.addClass('ser-li-current');
                    } else {
                        var this_el_index = obj_current.index();
                        if ((this_el_index - 1) >= 0) {
                            obj_current = obj_current.removeClass('ser-li-current').prev().addClass('ser-li-current');
                        } else {
                            if (obj_current.parents('.ser-row').prev().hasClass('ser-row')) {
                                obj_current = obj_current.removeClass('ser-li-current').parents('.ser-row').prev().find(".ser-m-con .ser-li:last").addClass('ser-li-current');
                            }
                        }
                    }
                    var em_html = obj_current.html();
                    obj_current.find('.ser-m-con-right').remove();
                    var _html = obj_current.text();
                    obj_current.html(em_html);
                    $("#J_city").val(_html);
                    return false;
                }
                // 向下
                if (e.keyCode == 40) {
                    if (!obj_current.html()) {
                        obj_current = $("#J_sea_box .ser-row:eq(0) .ser-m-con .ser-li:eq(0)").addClass('ser-li-current');
                    } else {
                        var this_el_nums = obj_current.parents('.ser-m-con').find('.ser-li').length;
                        var this_el_index = obj_current.index() + 1;
                        if ((this_el_index + 1) <= this_el_nums) {
                            obj_current = obj_current.removeClass('ser-li-current').next().addClass('ser-li-current');
                        } else {
                            if (obj_current.parents('.ser-row').next().hasClass('ser-row')) {
                                obj_current = obj_current.removeClass('ser-li-current').parents('.ser-row').next().find(".ser-m-con .ser-li:eq(0)").addClass('ser-li-current');
                            }
                        }
                    }
                    var em_html = obj_current.html();
                    obj_current.find('.ser-m-con-right').remove();
                    var _html = obj_current.text();
                    obj_current.html(em_html);
                    $("#J_city").val(_html);
                    return false;
                }
                if(e.keyCode == 13){
					var chkin = $("#nav_from").val();
					var chkout = $("#nav_to").val();
					var room = 1;
					var adult = 2;
					var child = 0;
					var child_age = "";
					/*if(key_index == -1){
						var select_index = 0;
					}else{
						var select_index = key_index;
					}*/
					var type = li_obj.attr("data-type");
					var data_name = li_obj.attr("data-name");
					var data_id = li_obj.attr("data-id");
                    if(type == 'poi'){
                        poi_channel = "|"+li_obj.attr("data-channel");
                    }
					if(type == undefined){
						type = 'keywords';
						data_name = $("#J_city").val();
						data_id = 0;
					}
                    var current_index = li_obj.index();
                    var current_conp_index = li_obj.parents('.ser-row').index();
                    var iter_index = 1,total_sl_len = 0;
                    $('#J_sea_box .ser-row').each(function(){
                        var sl_len = $(this).find('.ser-m-con .ser-li').length;
                        if (iter_index == current_conp_index) {
                            current_index += total_sl_len;
                        }
                        total_sl_len += sl_len;
                        ++iter_index;
                    });
					tj && tj.sea_click("select|"+ data_name +"\tadmin\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+type+poi_channel+"|"+data_id+"\t"+current_index+"|"+total_sl_len);
					/*if(li_obj.eq(key_index).attr("data-last") == 1){
						var uri = "/cz_"+ encodeURIComponent($("#J_city").val());
						window.location.href = uri;
						return false;
					}*/
					li_obj.find('.ser-m-con-right').remove();
					$("#J_city").val(li_obj.text()).css({color : "#333"}).parent().removeClass("index-date-error");
					$("#J_sea_box").hide();
					$("#from").focus().parent().addClass("index-date-current");
					from.datepicker("show");
					is_alias = li_obj.attr("data-alias");
					search_type = li_obj.attr("data-type");
					search_id = li_obj.attr("data-id");
					search_city_id = li_obj.attr("data-cityid");
					search_url_name = li_obj.attr("data-urlname");
                    search_name = li_obj.attr("data-name");
                    //Add POI
                    if(search_type=='poi'){
                        search_lat = li_obj.attr("data-lat");
                        search_lan = li_obj.attr("data-lan");
                        poi_channel = "|"+li_obj.attr("data-channel");
                    }
					is_sea_val = 0;
					return false;
				}
            }
            search_is_complete = 0;
            stamp = (new Date()).valueOf();
            search(val);
        }, 100);
    });
    $(".J_room_typs").live("mouseenter", function(){
        $(this).addClass("hotel-room-type-hover");
    })
    $(".J_room_typs").live("mouseleave", function(){
        $(this).removeClass("hotel-room-type-hover");
    });
    /*$("#J_sea_box li").live("click", function(){
        sea_timer && clearTimeout(sea_timer);
        tmp_ajax.abort();
        restartflag = 0;
        search_is_complete = 0;
        if($(this).attr("data-last") == 1){
            $("#J_city").val(sea_word);
            var uri = "/cz_"+ encodeURIComponent(sea_word);
			window.location.href = uri;
            return false;
        }
        $("#J_city").val($(this).attr("data-name")).css({color : "#333"}).parent().removeClass("index-date-error");
        $("#J_sea_box").hide();
        //增加alias
        is_alias = $(this).attr("data-alias");
        search_type = $(this).attr("data-type");
        search_id = $(this).attr("data-id");
        search_city_id = $(this).attr("data-cityid");
        search_url_name = $(this).attr("data-urlname");
        search_name = $(this).attr("data-name");
        //Add POI
        if(search_type=='poi'){
            search_lat = $(this).attr("data-lat");
            search_lan = $(this).attr("data-lan");
            poi_channel = "|"+$(this).attr("data-channel");
        }
        var chkin = $("#nav_from").val();
        var chkout = $("#nav_to").val();
        var room = 1;
        var adult = 2;
        var child = 0;
        var child_age = "";
        tj && tj.sea_click("select|"+ $(this).attr("data-name") +"\tadmin\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+search_type+poi_channel+"|"+search_id+"\t"+parseInt($(this).index()+1)+"|"+parseInt($("#J_sea_box li").length-1));
        is_sea_val = 0;
    });
    $("#J_sea_box li").live("mouseover", function(){
        key_index = $("#J_sea_box li").index(this);
        $(this).addClass("current").siblings().removeClass("current");
    });*/
    var sea_timer = false;
    var restartflag = 0;
    var search_is_complete = 0;
    var search_ajax_start = 0;
    var stamp = (new Date()).valueOf();
    var stamp_back = (new Date()).valueOf();
    //搜索框检索
    function search(val){
        if(val.replace(/\s*/g, "") != ""){
            if((is_ajax == 1 || val == sea_word) && search_is_complete){
                return false;
            }
            //poi is_complete = false request(s)
            if(val==sea_word){
                if(restartflag==1){
                    var search_ajax_now = Date.parse(new Date());
                    var cha = parseInt(search_ajax_now - search_ajax_start);
                    if(cha > 5000){
                        search_is_complete =1;
                        restartflag=0;
                        return false;
                    }
                }
            }
            key_index = -1;
            sea_word = val;
            is_sea_val = 1;
            $("#J_city").css({color : "#333"});
            is_ajax = 1;
            sea_timer && clearTimeout(sea_timer);
            search_type = "";
            tmp_ajax = HQ.ajax("/ajax/search_v1", {keyword : val, type : "",stamp:stamp}, function(data){
                $("#J_popup_city").hide();
                $("#J_sea_box").show();
                var html = '<div class="index-ser-top-title">'+HQ.config.language.admin_tips[language][0]+'</div>';
                if(data.msg == 0 || data.res == ""){
                    html += '<div style="padding: 6px 66px 6px 10px;background: #fff;color: #c00;cursor: default;background: none;line-height: normal;">找不到……用英文名试试看？</div>';
                    if(data.stamp>=stamp_back) {
                        $("#J_sea_box").html(html);
                    }
                }else{
                    if(data.stamp>=stamp_back) {
                        $("#J_sea_box").html(data.res);
                        // 选中第一个搜索项
                        $("#J_sea_box .ser-row:eq(0) .ser-m-con .ser-li:eq(0)").addClass("ser-li-current");
                        stamp_back = data.stamp;
                    }
                }
                search_is_complete = data.is_complete;
                if(search_is_complete){
                    restartflag = 0;
                }
                if(restartflag==0 && !search_is_complete && val==sea_word){
                    search_ajax_start = Date.parse(new Date());
                    restartflag = 1;
                }
                sea_timer = setTimeout(function(){
                    if($("#J_city").val() != sea_word || (!search_is_complete && restartflag==1)){
                        stamp = (new Date()).valueOf();
                        search($("#J_city").val());
                    }
                }, 300);
                is_ajax = 0;
            });
        }else{
            $("#J_sea_box").hide().html("");
            $("#J_city").css({color : "#9a9a9a"});
            $("#J_popup_city").show();
            is_sea_val = 0;
            sea_word = "";
        }
    }

    //支持placeholder
    var isInputSupported = 'placeholder' in document.createElement('input');
    if(true){
        /*$("#J_date input").each(function(){
            if($(this).val() == ""){
                $(this).val($(this).attr("data-placeholder"));
            }
        });
        $("#J_date input").focus(function(){
            if($(this).val() == $(this).attr("data-placeholder")){
                $(this).val("");
            }
        }).blur(function(){
            if($(this).val() == ""){
                $(this).val($(this).attr("data-placeholder"));
            }
        });*/
        if($("#J_city").val() == ""){
            $("#J_city").val($("#J_city").attr("data-placeholder")).css("color","#999");
        }
        $("#J_city").focus(function(){
            if($(this).val() == $(this).attr("data-placeholder")){
                $(this).val("");
            }
        }).blur(function(){
            if($(this).val() == ""){
                $(this).val($(this).attr("data-placeholder")).css("color","#999");
            }
        });
    }


    var city_id = config.city;
    var _city_id = city_id;

    $("#J_search_hotel").click(function(){
        if($("#J_sea_box li").attr("data-nores") == 1){
            $("#J_city").focus();
            return false;
        }
        if($("#J_city").val().replace(/\s/g, "") == "" || $("#J_city").val() == $("#J_city").attr("data-placeholder")){
            $("#J_city").focus().parent().addClass("index-date-error");
            return false;
        }

        if($("#from").val() == "" || $("#from").val() == $("#from").attr("data-placeholder")){
            $("#from").focus().parent().addClass("index-date-error");
            from.datepicker("show");
            return false;
        }else if($("#to").val() == "" || $("#to").val() == $("#to").attr("data-placeholder")){
            $("#to").focus().parent().addClass("index-date-error");
            to.datepicker("show");
            return false;
        }
        if(!$("#J_sea_box").is(":hidden")){
            var li_obj = $("#J_sea_box .ser-li");
            if(li_obj.attr("data-nores") != 1){
                li_obj.eq(0).find('.ser-m-con-right').remove();
                $("#J_city").val(li_obj.eq(0).text()).css({color : "#333"}).parent().removeClass("index-date-error");

                $("#J_city_hid").val(li_obj.eq(0).attr("data-cityid"));
                $("#J_sea_box").hide();

                is_alias = li_obj.eq(0).attr("data-alias");
                search_type = li_obj.eq(0).attr("data-type");
                search_id = li_obj.eq(0).attr("data-id");
                search_city_id = li_obj.eq(0).attr("data-cityid");
                search_url_name = li_obj.eq(0).attr("data-urlname");
                if(search_type == 'poi'){
                    poi_channel = "|"+li_obj.eq(0).attr("data-channel");
                }
                $("#J_tips").hide();

                var chkin = $("#from").val();
                var chkout = $("#to").val();
                // var room = $("#J_top_sel_room_num").val();
                // var adult = $("#J_top_sel_room_adult").val();
                // var child = $("#J_top_sel_room_child").val();
                // var child_age = $("#J_top_sel_room_child_age").val();
                var room = 1;
                var adult = 2;
                var child = 0;
                var child_age = "";
                var total_sl_len = 0;
                $('#J_sea_box .ser-row').each(function(){
                    total_sl_len += $(this).find('.ser-m-con .ser-li').length;
                });

                tj && tj.sea_click("select|"+ li_obj.eq(0).attr("data-name") +"\thotelmap\t"+config.dist_id+"|"+config.user_id+"|"+chkin+"|"+chkout+"|"+room+"|"+adult+"|"+child+"|"+child_age+"|\t"+$("#J_city").val()+"\t"+search_type+poi_channel+"|"+search_id+"\t1|"+total_sl_len);
            }
        }
        search_jump("J_city");

        return false;

    });

    function search_jump(city_obj){

        var uri = (HQ.config.base_url ? HQ.config.base_url : '') + "/";

        var token = $("#J_token").val();
        if(token != ""){
            token = "&token="+token;
        }
        if(city_obj == "J_city"){
            var chkin = $("#from").val();
            var chkout = $("#to").val();
        }else{
            var chkin = config.chkin;
            var chkout = config.chkout;
        }

        var room = 1;
        var adult = 2;
        var child = 0;
        var child_age = "";

        if(search_type == "country"){
            uri += search_url_name +".html?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else if(search_type == "region"){
            uri += search_url_name +".html?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else if(search_type == "city"){
            uri += search_url_name +"_c"+ search_city_id +"/?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else if(search_type == "area"){
            uri += search_url_name +"_c"+ search_city_id +"/a"+ search_id +"?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else if(search_type == "attr"){
            uri += search_url_name +"_c"+ search_city_id +"/attr"+ search_id +"?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else if(search_type == "hotel"){
            uri += search_url_name +"_c"+ search_city_id +"/"+ search_id +".html?";
            if(is_alias == 1){
                uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + "&alias=" + encodeURIComponent($("#"+ city_obj).val()) + token;
            }else{
                uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
            }
        }else if(search_type == "poi"){
            uri += search_url_name +"_c"+ search_city_id +"?";
            uri = uri +"checkin="+ chkin +"&checkout="+ chkout + "&type=" + search_type + "&lat="+ search_lat + "&lng=" + search_lan + "&dataname=" + search_name + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
        }else{
            if(city_obj == "J_city" && $("#"+ city_obj).val() == config.name){
                uri = "/"+ $("#J_url_name").val() +"_c"+ config.city +"/?checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
            }else{
                uri = "/cz_"+ encodeURIComponent($("#"+ city_obj).val()) +"?checkin="+ chkin +"&checkout="+ chkout + "&room=" + room + "&adult=" + adult + "&child=" + child + "&child_age=" + child_age + token;
            }
        }
        HQ.ajax("/ajax/set_date", {chkin : chkin, chkout : chkout,room_num:room,adult:adult,child:child,child_age:child_age});

        window.setTimeout(function(){
            window.location.href = uri;
        },500);
    }


    //日期
    var date = "";
    var max_date = new Date(new Date().getTime() + 365 * 24 * 60 * 60 * 1000);
    var from = $("#nav_from").datepicker({
        dateFormat : "yy-mm-dd",
        minDate : new Date(),
        maxDate : max_date,
        to : "nav_to",
        numberOfMonths: 2,
        dayNamesMin: week,
        monthNames: month,
        monthNamesShort: month,
        showAnim : "",
        onSelect : function(selectedDate){
            var time = selectedDate.split("-");
            var time1 = [];
            $("#nav_to").datepicker( "option", "minDate", selectedDate);
            time1[0] = time[0];
            time1[1] = time[1] - 1;
            time1[2] = time[2];
            if(time[1] == 12){
                time[0] = parseInt(time[0])+1;
                time[1] = 0;
                if(time[2] == 31){
                    time1[2] = 1;
                    time1[1] = 0;
                    time1[0] = time[0];
                }else{
                    time1[2] = parseInt(time[2]) + 1;
                }
            }else{
                time1[2] = parseInt(time[2]) + 1;
            }
            if(time[2] > 28){
                time[2] = 28;
            }
            $("#nav_from").css({color : "#333"});
            $("#nav_to").css({color : "#333"});
            $("#nav_to").val(time1[0] +"-"+ (parseInt(time1[1])+1) +"-"+ time1[2]);
            $("#nav_to").datepicker( "option", "minDate", new Date(time1[0], time1[1], time1[2]));
            $("#nav_to").datepicker( "option", "maxDate", new Date(time[0], time[1], time[2]));
            setTimeout(function(){
                to.datepicker("show");
            }, 10);
        }
    });
    var min_de_date = new Date($("#nav_from").val());
    var min_de_month = min_de_date.getMonth();
    var min_de_year = min_de_date.getFullYear();
    var max_de_month = parseInt(min_de_month) + 1;
    var max_de_year = min_de_year;
    if (max_de_month == 13) {
        max_de_year = parseInt(min_de_year) + 1;
    }
    var max_de_date = new Date(max_de_year, max_de_month, min_de_date.getDate());
    var to = $("#nav_to").datepicker({
        dateFormat : "yy-mm-dd",
        minDate : min_de_date,
        maxDate : max_de_date,
        from : "nav_from",
        dateMouse : true,
        numberOfMonths: 2,
        dayNamesMin: week,
        monthNames: month,
        showAnim : "",
        monthNamesShort: month
    });

    $("body").click(function(){
        $("#J_popup_city").hide();
        if(!$("#J_sea_box").is(":hidden")){
            var li_obj = $('#J_sea_box .ser-row .ser-m-con .ser-li');
            if(li_obj.attr("data-nores") != 1){
                li_obj.eq(0).find('.ser-m-con-right').remove();
                $("#J_city").val(li_obj.eq(0).text()).css({color : "#333"}).parent().removeClass("index-date-error");

                $("#J_city_hid").val(li_obj.eq(0).attr("data-cityid"));
                $("#J_sea_box").hide();

                is_alias = li_obj.eq(0).attr("data-alias");
                search_type = li_obj.eq(0).attr("data-type");
                search_id = li_obj.eq(0).attr("data-id");
                search_city_id = li_obj.eq(0).attr("data-cityid");
                search_url_name = li_obj.eq(0).attr("data-urlname");
                $("#J_tips").hide();

                tj && tj.sea_click("select|"+ li_obj.eq(0).attr("data-name") +"\thotelmap");
            }

        }
        $("#J_sea_box").hide();

    });
    //app pop
    var login_menu_timer = "";
    $("#J_nav_menu_app").mouseenter(function(){
        $(".J_header_icon_app").attr("src",config.static_img+"/website/images/header_icon_app_hover.png");
        var _this = $(this);
        clearTimeout(login_menu_timer);
        login_menu_timer = setTimeout(function(){
            $("#J_nav_menu_qr").removeClass("nav-menu-show");
        }, 300);
        login_menu_timer = setTimeout(function(){
            _this.addClass("nav-menu-show");
        }, 300);

    }).mouseleave(function(){
        $(".J_header_icon_app").attr("src",config.static_img+"/website/images/header_icon_app.png");
        var _this = $(this);
        login_menu_timer = setTimeout(function(){
            _this.removeClass("nav-menu-show");
        }, 300);
    });

    $("#J_nav_app").click(function(){
        window.open("/app?type=top_click");
    });

    $("#J_go_app").click(function(){
        window.open("/app?type=top_qr");
    });
    //qr pop
    $("#J_nav_menu_qr").mouseenter(function(){
        $(".J_header_icon_qr").attr("src",config.static_img+"/website/images/header_icon_qr_hover.png");
        var _this = $(this);
        clearTimeout(login_menu_timer);
        login_menu_timer = setTimeout(function(){
            $("#J_nav_menu_app").removeClass("nav-menu-show");
        }, 300);
        login_menu_timer = setTimeout(function(){
            _this.addClass("nav-menu-show");
        }, 300);

    }).mouseleave(function(){
        $(".J_header_icon_qr").attr("src",config.static_img+"/website/images/header_icon_qr.png");
        var _this = $(this);
        login_menu_timer = setTimeout(function(){
            _this.removeClass("nav-menu-show");
        }, 300);
    });

    $(".pre_sale_qq").mouseover(function(){
        $(".pre_sale_qq").find("a").css("color","#49f");
        $(".J_header_icon_qq").attr("src",config.static_img+"/website/images/kefu/kefu_25_hover.png?v2.00");
    }).mouseout(function(){
        $(".pre_sale_qq").find("a").css("color","#666");
        $(".J_header_icon_qq").attr("src",config.static_img+"/website/images/kefu/kefu_25.png?v2.00");
    });



    var dist_id = 0;
    var dist_user_id = 0;
    var uname = "";
    var realname = "";
    var partnerId = "";


	//zhichi
	var zhiManager = (getzhiSDKInstance());
	zhiManager.on("load", function() {zhiManager.initBtnDOM();});
	zhiManager.set('customMargin', 20);
	var inventorySearch = $.cookie("sea_arr");

	inventorySearch = eval("("+inventorySearch+")");
	zhiManager.set("partnerId",partnerId);
	zhiManager.set("uname",uname);
	zhiManager.set("realname",realname);
  if (inventorySearch) {
    zhiManager.set("params",JSON.stringify({
  		'dist_id':dist_id,
  		'dist_user_id':dist_user_id,
      'checkin': inventorySearch['checkin'],
  		'checkout': inventorySearch.checkout,
  		'room':inventorySearch.room,
  		'adult': inventorySearch.adult,
  		'child': inventorySearch.child,
  		'child_age': inventorySearch.child_age,
  		'url': window.location.href
  	}));
  }
    // 左侧菜单栏
    var isAnimate = false;
    $('.menu-no-single .menu-first-catalog').on('click',function () {
        if (isAnimate) return;
        var nextBox = $(this).next(),
            parentBox = nextBox.parent('.menu-no-single');
        isAnimate = true;
        if (nextBox.is(':hidden')) {
            parentBox.removeClass('close');
            nextBox.slideDown('fast', function () {
                isAnimate = false;
            });
        } else {
            parentBox.addClass('close');
            nextBox.slideUp('fast',function () {
                isAnimate = false;
            });
        }
    })
})();
